//
//  ViewController.m
//  TestPrj
//
//  Created by 陈晓星 on 2019/7/9.
//  Copyright © 2019 Xing. All rights reserved.
//

#import "HomeViewController.h"
#import "TableViewDataModel.h"
#import "HomeCellTableViewCell.h"
#import <Masonry.h>
#import <HBDNavigationBar/HBDNavigationController.h>
#import "SEHomeViewController.h"
#import "ReactiveObjCViewController.h"
#import "HotFixViewController.h"
#import "JFLiveShowVC.h"
#import "WaveTextViewController.h"
#import "WaterFallByCollectionViewVC.h"
#import "VcTransitionsTableViewController.h"
#import "SafeProxyTestViewController.h"
#import "SlipBackNavigationViewController.h"
#import "VideoTestViewController.h"
#import "UISearchBarTestViewController.h"
#import <Toast.h>
#import "DynamicChangeIconViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import "ChatRoomHomeViewController.h"
#import "SegmentDownloadViewController.h"
#import "AssemblyTestViewController.h"
#import "ImageEffectTestViewController.h"

@interface HomeViewController ()

@property(nonatomic,strong) UITableView *tablView;
@property(nonatomic,strong) TableViewDataModel *model;

@property(nonatomic,strong) NSArray *dataSources; 

@end

@implementation HomeViewController

- (void)loadView {
    
    [super loadView];
    
    _tablView = [[UITableView alloc] init];
    _tablView.backgroundColor = [UIColor clearColor];
    [_tablView registerClass:[HomeCellTableViewCell class] forCellReuseIdentifier:@"HomeCellTableViewCell"];
    _tablView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tablView];
    
    [_tablView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _dataSources = @[@"Masonry、ReactiveObjC、多线程代码",
                     @"Aspects Test",
                     @"滚动视图效果",
                     @"视频相关",
                     @"WaveText(波浪经过文字，文字改变颜色)",
                     @"瀑布流（自定义UICollectionViewLayout）",
                     @"VcTransitions(VC转场)",
                     @"防止应用闪退的实现方式一",
                     @"UISearchBar测试",
                     @"动态更改应用图标",
                     @"仿网易云讯的IM代码",
                     @"断点下载",
                     @"逆向相关",
                     @"图像处理"
    ];
    
    __weak typeof(self) weakself = self;
    
    CellModel *cellModel = [[CellModel alloc] init];
    cellModel.Cell = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
        HomeCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeCellTableViewCell" forIndexPath:indexPath];
        NSString *name = [weakself.dataSources objectAtIndex:indexPath.row];
        [cell loadData:name];
        return cell;
    };
    cellModel.CellHeight = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
        return UITableViewAutomaticDimension;
    };
    cellModel.SelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
        [weakself pushActionWithIndex:indexPath.row];
    };
    
    TableViewSectionModel *sectionModel = [[TableViewSectionModel alloc] init];
    sectionModel.cellModel = cellModel;
    sectionModel.numberOfRowsInSection = ^NSInteger(UITableView *tableView, NSInteger section) {
        return weakself.dataSources.count;
    };
    
    _model = [[TableViewDataModel alloc] init];
    [_model targetTableView:_tablView];
    [_model.tableViewDataArr addObject:sectionModel];
    
    WEAKSELF
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf checkNetworkProxy];
    });
}

/// 检查手机网络是否设置了代理
- (void)checkNetworkProxy {
    
    [Utils getProxyStatus:^(BOOL isProxy, NSString * _Nonnull hostName, NSString * _Nonnull portNum, NSString * _Nonnull proxyType) {
        if (isProxy) {
            NSString *tip = [NSString stringWithFormat:@"检测到您的手机设置了网络代理\n(%@ %@:%@)",proxyType,hostName,portNum];
            [self.view makeToast:tip];
        }
    }];
}

- (void)pushActionWithIndex:(NSInteger)index {
    
    [self feedbackAction];
    
    if (index == 0) { //Masonry和ReactiveObjC

        ReactiveObjCViewController *vc = [[ReactiveObjCViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];

    } else if (index == 1) { //Aspects Test

        HotFixViewController *vc = [HotFixViewController new];
        [self.navigationController pushViewController:vc animated:YES];

    } else if (index == 2) { //滚动视图效果

        SEHomeViewController *seHomeVC = [[SEHomeViewController alloc] init];
        SlipBackNavigationViewController *nav = [[SlipBackNavigationViewController alloc] initWithRootViewController:seHomeVC];
        if (@available(iOS 13.0,*)) {
            //在iOS13中modalPresentationStyle的默认改为UIModalPresentationAutomatic,类似卡片式
            nav.modalPresentationStyle = UIModalPresentationFullScreen;
        }
        [self presentViewController:nav animated:YES completion:nil];

    } else if (index == 3) { //JFLivePlayer录屏直播

        VideoTestViewController *vtVC = [[VideoTestViewController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vtVC];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:nav animated:YES completion:nil];

    } else if (index == 4) { //WaveText(波浪经过文字，文字改变颜色)

        WaveTextViewController *vc = [WaveTextViewController new];
        [self.navigationController pushViewController:vc animated:YES];

    } else if (index == 5) { //瀑布流（自定义UICollectionViewLayout）
        
        WaterFallByCollectionViewVC *vc = [WaterFallByCollectionViewVC new];
        [self.navigationController pushViewController:vc animated:YES];
    
    } else if (index == 6) { //VcTransitions(VC转场)
        
        VcTransitionsTableViewController *vc = [[VcTransitionsTableViewController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:nav animated:YES completion:nil];
    
    } else if (index == 7) { //防止应用闪退的实现方式一
        
        SafeProxyTestViewController *vc = [SafeProxyTestViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    
    } else if (index == 8) { //UISearchBar测试
        
        UISearchBarTestViewController *vc = [[UISearchBarTestViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    
    } else if (index == 9) { //动态更改图标
        
        DynamicChangeIconViewController *vc = [[DynamicChangeIconViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    
    } else if (index == 10) { //仿网易云讯的IM代码
        
        ChatRoomHomeViewController *seHomeVC = [[ChatRoomHomeViewController alloc] init];
        SlipBackNavigationViewController *nav = [[SlipBackNavigationViewController alloc] initWithRootViewController:seHomeVC];
        if (@available(iOS 13.0,*)) {
            //在iOS13中modalPresentationStyle的默认改为UIModalPresentationAutomatic,类似卡片式
            nav.modalPresentationStyle = UIModalPresentationFullScreen;
        }
        [self presentViewController:nav animated:YES completion:nil];
    
    } else if (index == 11) { //断点下载
        
        SegmentDownloadViewController *vc = [SegmentDownloadViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    
    } else if (index == 12) { //逆向相关
        
        AssemblyTestViewController *vc = [AssemblyTestViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    
    } else if (index == 13) { //图像处理
        
        ImageEffectTestViewController *ieVC = [ImageEffectTestViewController new];
        HBDNavigationController *nav = [[HBDNavigationController alloc] initWithRootViewController:ieVC];
        
        if (@available(iOS 13.0,*)) {
            //在iOS13中modalPresentationStyle的默认改为UIModalPresentationAutomatic,类似卡片式
            nav.modalPresentationStyle = UIModalPresentationFullScreen;
        }
        [self presentViewController:nav animated:YES completion:nil];
    }
}

/// 交互震动反馈
static int kFeedBackCount = 0;
- (void)feedbackAction {
    
    int index = kFeedBackCount++%8;
    NSLog(@"feedbackAction.index:%d",index);
    switch (index) {
        case 0:
        {
            //普通短震（类似3D Touch的 Peek 反馈 ）
            AudioServicesPlaySystemSound(1519);
            break;
        }
        case 1:
        {
            //普通短震 （类似3D Touch Pop 反馈）
            AudioServicesPlaySystemSound(1520);
            break;
        }
        case 2:
        {
            //连续三次短震
            AudioServicesPlaySystemSound(1521);
            break;
        }
        case 3:
        {
            if (@available(iOS 10.0, *)) {
                UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
                [generator prepare];
                [generator impactOccurred];
            }
            break;
        }
        case 4:
        {
            if (@available(iOS 10.0, *)) {
                UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium];
                [generator prepare];
                [generator impactOccurred];
            }
            break;
        }
        case 5:
        {
            if (@available(iOS 10.0, *)) {
                UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleHeavy];
                [generator prepare];
                [generator impactOccurred];
            }
            break;
        }
        case 6:
        {
            if (@available(iOS 13.0, *)) {
                UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleSoft];
                [generator prepare];
                [generator impactOccurredWithIntensity:1.0];
            }
            break;
        }
        case 7:
        {
            if (@available(iOS 13.0, *)) {
                UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleRigid];
                [generator prepare];
                [generator impactOccurredWithIntensity:1.0];
            }
            break;
        }
        default:
            break;
    }
}

@end
